//
//  IntentViewController.m
//  HMIntentUI
//
//  Created by orvibo on 2018/12/26.
//  Copyright © 2018 orvibo. All rights reserved.
//

#import "IntentViewController.h"
#import "HMSceneIntent.h"
#import "HMIntentUIHeader.h"

// As an example, this extension's Info.plist has been configured to handle interactions for INSendMessageIntent.
// You will want to replace this or add other intents as appropriate.
// The intents whose interactions you wish to handle must be declared in the extension's Info.plist.

// You can test this example integration by saying things to Siri like:
// "Send a message using <myApp>"

@interface IntentViewController ()

@property (weak, nonatomic) IBOutlet UILabel *alertLabel;

@end

@implementation IntentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [HMSDK setDebugEnable:YES];
    [HMSDK initWithAppName:@"ZhiJia365"];
    // 登录账号
    [self loginAccount:^(KReturnValue returnValue, NSDictionary *returnDic) {
        
    }];
}

-(void)loginAccount:(SocketCompletionBlock)completion
{
    // 同步所有数据，然后刷新显示
    NSString *userName = @"18620330995";
    NSString *password = md5WithStr(@"123456");

    [userAccout() widgetLoginWithUserName:userName password:password completion:^(KReturnValue value) {
        NSLog(@"登录%@",(value == KReturnValueSuccess) ? @"成功" : @"失败");
        
        if (value == KReturnValueSuccess) { // 登录成功，保存该账号
            NSLog(@"widgetScene: 登录成功");
        } else if (value == KReturnValueAccountOrPWDERR) {
            NSLog(@"widgetScene: 用户名或密码错误");

        } else if(value == KReturnValueFamilyInvalid) {
            NSLog(@"widgetScene: 家庭失效(家庭已不存在)");
            // 家庭失效则删除已保存的情景数据
        }
    }];
    
}
#pragma mark - INUIHostedViewControlling

// Prepare your view controller for the interaction to handle.
- (void)configureViewForParameters:(NSSet <INParameter *> *)parameters ofInteraction:(INInteraction *)interaction interactiveBehavior:(INUIInteractiveBehavior)interactiveBehavior context:(INUIHostedViewContext)context completion:(void (^)(BOOL success, NSSet <INParameter *> *configuredParameters, CGSize desiredSize))completion {
    // Do configuration here, including preparing views and calculating a desired size for presentation.
    
    if ([interaction.intent isKindOfClass:[HMSceneIntent class]]) {
        HMSceneIntent *intent = (HMSceneIntent *)interaction.intent;
        
        [HMSceneAPI sceneControlWithSceneNo:intent.sceneNo sceneId:intent.sceneId.intValue uidArray:intent.uidArray completion:^(KReturnValue value, id object) {
            
        }];
        
        self.alertLabel.text = [NSString stringWithFormat:@"%@ 执行成功",intent.sceneName];
        NSLog(@"%@",intent);
    }
    
    if (completion) {
        completion(YES, parameters, CGSizeMake([self desiredSize].width, 120.0f));
    }
}

- (CGSize)desiredSize {
    return [self extensionContext].hostedViewMaximumAllowedSize;
}

@end
